package com.kgc.javaweb.service.impl;

import com.kgc.javaweb.dao.OwnerDao;
import com.kgc.javaweb.dao.impl.OwnerDaoImpl;

import com.kgc.javaweb.entity.Owner;
import com.kgc.javaweb.enums.CodeMsg;
import com.kgc.javaweb.service.OwnerService;
import com.kgc.javaweb.util.ResponseResult;

public class OwnerServiceImpl implements OwnerService {
    OwnerDao ownerDao=new OwnerDaoImpl();

    @Override
    public ResponseResult selectOwnerById(String id) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            Owner owner = ownerDao.selectById(id);
            responseResult.setData(owner);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult update(Owner owner) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            ownerDao.update(owner);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult repair(String name, String address) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            ownerDao.repair(name,address);
            responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
            responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
            responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
        }
        return responseResult;
    }

    @Override
    public ResponseResult updateUserInfoPwdById(String newPassword, Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();

        try {
            ownerDao.updateUserInfoPwdById(newPassword,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public Owner ownerLogin(String username, String password) {
        Owner owner = ownerDao.findOwnerByUserNameAndPassword(username, password);

        return owner;
    }
}
